package cn.hdu.liang.core.controller;

import cn.hdu.liang.common.web.session.SessionProvider;
import cn.hdu.liang.core.bean.product.*;
import cn.hdu.liang.core.query.product.BrandQuery;
import cn.hdu.liang.core.query.product.FeatureQuery;
import cn.hdu.liang.core.query.product.ProductQuery;
import cn.hdu.liang.core.query.product.TypeQuery;
import cn.hdu.liang.core.service.product.*;
import cn.itcast.common.page.Pagination;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Controller
public class FrontProductController {

    @Autowired
    private BrandService brandService;
    @Autowired
    private ProductService productService;

    @Autowired
    private TypeService typeService;

    @Autowired
    private FeatureService featureService;

    @Autowired
    private SkuService skuService;

    @Autowired
    private SessionProvider sessionProvider;

    //商品列表页面
    @RequestMapping(value = "/product/display/list.shtml")
    public String list(ModelMap model, Integer brandId, String brandName, Integer typeId, String typeName, Integer pageNo) {

        //加载商品属性
        FeatureQuery featureQuery = new FeatureQuery();
        List<Feature> features = featureService.getFeatureList(featureQuery);
        model.addAttribute("features", features);

        //分页参数
        StringBuilder params = new StringBuilder();
        //设置页号
        ProductQuery productQuery = new ProductQuery();
        productQuery.setPageNo(Pagination.cpn(pageNo));
        productQuery.setPageSize(Product.FRONT_PAGE_SIZE);
        productQuery.orderbyId(false);

        //隐藏已选条件
        boolean flag = false;

        //条件Map窗口
        Map<String, String> query = new LinkedHashMap<>();

        //品牌ID
        if(brandId != null) {
            productQuery.setBrandId(brandId);
            flag = true;
            query.put("品牌", brandName);
            model.addAttribute("brandId", brandId);
            model.addAttribute("brandName", brandName);
            params.append("&brandId=").append(brandId).append("&brandName=").append(brandName);
        } else {
            //加载商品品牌
            BrandQuery brandQuery = new BrandQuery();
            brandQuery.setFields("id,name");
            brandQuery.setIsDisplay(1);
            List<Brand> brands = brandService.getBrandList(brandQuery);
            model.addAttribute("brands", brands);
        }

        //类型ID
        if(typeId != null) {
            productQuery.setTypeId(typeId);
            flag = true;
            query.put("类型", typeName);
            model.addAttribute("typeId", typeId);
            model.addAttribute("typeName", typeName);
            params.append("&typeId=").append(typeId).append("&typeName=").append(typeName);
        } else {
            //加载商品类型
            TypeQuery typeQuery = new TypeQuery();
            //指定查询字段
            typeQuery.setFields("id,name");
            typeQuery.setIsDisplay(1);
            typeQuery.setParentId(0);
            List<Type> types = typeService.getTypeList(typeQuery);
            model.addAttribute("types", types);
        }
        model.addAttribute("flag", flag);
        model.addAttribute("query", query);

        //加载带有分页的商品
        Pagination pagination = productService.getProductListWithPage(productQuery);

        //分页页面展示
        String url = "/product/display/list.shtml";
        pagination.pageView(url, params.toString());
        model.addAttribute("pagination", pagination);

        return "product/product";
    }

    //跳转商品详情页
    @RequestMapping(value = "/product/detail.shtml")
    public String detail(Integer id, ModelMap model) {
        //商品加载
        Product product = productService.getProductByKey(id);
        model.addAttribute("product", product);

        //skus
        List<Sku> skus = skuService.getStock(id);
        model.addAttribute("skus", skus);

        ////去重复
        //List<Color> colors = new ArrayList<>();
        ////遍历Sku
        //for(Sku sku : skus) {
        //    Color c = sku.getColor();
        //    if(!colors.contains(c)) {
        //        colors.add(c);
        //    }
        //}
        List<Color> colors = skus.stream().map(Sku::getColor).distinct().collect(Collectors.toList());
        model.addAttribute("colors", colors);

        return "product/productDetail";
    }
}
